home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 6 / develop 6 code / TCP / NewsWatcher / NewsWatcher 2.0d15 source / source / send.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-01  |  3.6 KB  |  186 lines  |  [TEXT/KAHL]

  1. /*----------------------------------------------------------------------------
  2.  
  3.     send.c
  4.  
  5.     This module handles sending postings and mail messages.
  6.     
  7.     Portions copyright © 1990, Apple Computer.
  8.     Portions copyright © 1993, Northwestern University.
  9.  
  10. ----------------------------------------------------------------------------*/
  11.  
  12. #include <string.h>
  13.  
  14. #include "glob.h"
  15. #include "dlgutil.h"
  16. #include "nntp.h"
  17. #include "save.h"
  18. #include "send.h"
  19. #include "smtp.h"
  20. #include "util.h"
  21.  
  22.  
  23.  
  24. #define kAskSendAlert        134            /* Send confirm dialog */
  25.  
  26.  
  27.  
  28. /*    CheckForSend asks the user if he/she would like to send the message
  29.     that they composed before the window has been closed.
  30. */
  31.  
  32. Boolean CheckForSend (WindowPtr wind)
  33. {
  34.     TWindow **info;
  35.     Handle Hdl;
  36.     DialogPtr dlg;
  37.     short item;
  38.  
  39.     info = (TWindow**)GetWRefCon(wind);
  40.     Hdl = GetResource('DITL',kAskSendAlert);
  41.     
  42.     HNoPurge(Hdl);
  43.     if((**info).kind == kPostMessage)
  44.     {
  45.         strncpy(&((*Hdl)[16]),"Post",4);
  46.         ParamText("\pPost","\p","\p","\p");
  47.     }
  48.     else
  49.     {
  50.         strncpy(&((*Hdl)[16]),"Mail",4);
  51.         ParamText("\pMail","\p","\p","\p");
  52.     }
  53.     dlg = MyGetNewDialog(kAskSendAlert);
  54.     SetItemKeyEquivalent(dlg, 3, 'D');
  55.     MyModalDialog(DialogFilter,&item,true,true);
  56.     MyDisposDialog(dlg);
  57.     
  58.     switch (item) {
  59.         case 1: /* send */
  60.             return DoSendMsg(wind);
  61.             break;
  62.         case 2: /* cancel */
  63.             return false;
  64.         case 3: /* discard */
  65.             return true;
  66.     }
  67. }
  68.  
  69.  
  70. /*    WordWrap wraps paragraphs to lines of at most 75 characters long in all
  71.     outgoing messages. Only body lines which do not start with '>' are wrapped.
  72. */
  73.  
  74. static void WordWrap (Handle h)
  75. {
  76.     long offset, len;
  77.     char *p, *pEnd, *q, *lastSpace;
  78.     
  79.     offset = Munger(h,0,CRCR,2,nil,0);
  80.     if (offset < 0) return;
  81.     len = GetHandleSize(h);
  82.     p = *h + offset + 2;
  83.     pEnd = *h + len;
  84.     while (p < pEnd) {
  85.         if (*p == '>') {
  86.             while (p < pEnd && *p != CR) p++;
  87.             p++;
  88.         } else {
  89.             q = p;
  90.             lastSpace = 0;
  91.             while (true) {
  92.                 while (q < pEnd && *q != ' ' && *q != CR) q++;
  93.                 if (q - p >= 76 && lastSpace != 0) {
  94.                     *lastSpace = CR;
  95.                     p = lastSpace + 1;
  96.                     lastSpace = 0;
  97.                 }
  98.                 if (q >= pEnd || *q == CR) break;
  99.                 lastSpace = q;
  100.                 q++;
  101.             }
  102.             p = q+1;
  103.         }
  104.     }
  105. }
  106.  
  107.  
  108. /*    DoSendMsg either posts a message or sends the message through
  109.     electronic mail (SMTP) after converting the appropriate header
  110.     fields, etc…
  111. */
  112.  
  113. Boolean DoSendMsg (WindowPtr wind)
  114. {
  115.     TWindow **info;
  116.     Handle bufa=nil,bufb=nil;
  117.     unsigned long lena,lenb;
  118.     Ptr p,q,pEnd;
  119.     Boolean result;
  120.     OSErr err;
  121.     Boolean mustDispose;
  122.  
  123.     info = (TWindow**)GetWRefCon(wind);
  124.     if ((**info).kind == kPostMessage) {
  125.         StatusWindow("Posting message.");
  126.     } else {
  127.         StatusWindow("Mailing message.");
  128.     }
  129.     
  130.     GetFullMessageText(wind, &bufa, &mustDispose);
  131.     if (!mustDispose) {
  132.         if ((err = MyHandToHand(&bufa)) != noErr) {
  133.             bufa = nil;
  134.             goto exit;
  135.         }
  136.     }
  137.     lena = GetHandleSize(bufa);
  138.     
  139.     WordWrap(bufa);
  140.     
  141.     bufb = MyNewHandle(2*lena);
  142.     if ((err = MyMemErr()) != noErr) goto exit;
  143.     
  144.     /* Convert all CR to CRLF. Also convert all '.' at bol to '..'
  145.        Also strip all trailing CR's. */
  146.     
  147.     p = *bufa;
  148.     pEnd = p + lena;
  149.     while (pEnd > p && *(--pEnd) == CR);
  150.     pEnd++;
  151.     q = *bufb;
  152.     while (p < pEnd) {
  153.         if (*p == '.') *q++ = '.';
  154.         while (p < pEnd && *p != CR) *q++ = *p++;
  155.         if (p < pEnd) {
  156.             *q++ = CR;
  157.             *q++ = LF;
  158.             p++;
  159.         }
  160.     }
  161.     lenb = q - *bufb;
  162.     MyDisposHandle(bufa);
  163.     bufa = nil;
  164.  
  165.     /* Send message. */
  166.     
  167.     HLock(bufb);
  168.     switch ((**info).kind) {
  169.         case kMailMessage:
  170.             result = SendSMTP(*bufb,lenb);
  171.             break;
  172.         case kPostMessage:
  173.             result = PostArticle(*bufb,lenb);
  174.             break;
  175.     }
  176.     MyDisposHandle(bufb);
  177.     if (result) (**info).changed = false;
  178.     return result;
  179.     
  180. exit:
  181.  
  182.     UnexpectedErrorMessage(err);
  183.     MyDisposHandle(bufa);
  184.     MyDisposHandle(bufb);
  185.     return false;
  186. }